/* internal/js/employee/maintain.js */

/* Common */

var SERVLET_PATH = '/ordrs/internal/maintain-employee';

/* END Common */

/* Create */
function createEmployeeAccount()
{
	createEmployeeAccountValidate();
}

function createEmployeeAccountValidate()
{
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var passcode = document.getElementById('passcode').value;
	var posInd = document.employeeForm.position.selectedIndex;
	var position = document.employeeForm.position.options[posInd].text;
	
	var valid = true;
	

	if(firstname == '')
	{
		valid = false;
		showFieldRequired('firstNameMessage');
	}
	if(lastname == '')
	{
		valid = false;
		showFieldRequired('lastNameMessage');
	}
	if(passcode == 'j8&#4i^b/1!c')
	{
		passcode = '';
	}
	if(passcode == '')
	{
		valid = false;
		showFieldRequired('passCodeMessage');
	}
	if(position == '')
	{
		valid = false;
		showFieldRequired('positionMessage');
	}
	
	if (valid)
	{
		createEmployeeAccountSend(username,password,firstname,lastname,position,passcode);
	}
}

function createEmployeeAccountSend(username,password,firstname,lastname,position,passcode)
{
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('c') +
				'&username='+encodeURI(username)+
				'&password='+encodeURI(password)+
				'&firstname='+encodeURI(firstname)+
				'&lastname='+encodeURI(lastname)+
				'&position='+encodeURI(position)+
				'&passcode='+encodeURI(passcode);
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				createEmployeeAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				createEmployeeAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function createEmployeeAccountReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) 
	{
		listAllEmployeeAccountSend();
		showDialog('Employee Account created.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* Report */
function reportEmployeeAccount()
{
	reportEmployeeAccountValidate();
}

/* Done */
function reportEmployeeAccountValidate()
{
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var username = document.getElementById('username').value;
	var position = document.getElementById('position').value;
	reportEmployeeAccountSend(firstname,lastname,username,position);
}

function reportEmployeeAccountSend(firstname,lastname,username,position)
{
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('r') +
				'&firstname='+encodeURI(firstname)+
				'&lastname='+encodeURI(lastname)+
				'&username='+encodeURI(username)+
				'&position='+encodeURI(position);
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				reportEmployeeAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				reportEmployeeAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
	
}

function reportEmployeeAccountReceive (xml) {
	if (xml.getElementsByTagName('employees').length > 0) {
		var employees = xml.getElementsByTagName('employees')[0];
		showResults(buildTable(employees));
		if (xml.getElementsByTagName('employees').length == 1)
			showResult(xml.getElementsByTagName('id')[0].childNodes[0].nodeValue);
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}
/* End Report */

/* Update */
function updateEmployeeAccount()
{
	updateEmployeeAccountValidate();
}

function updateEmployeeAccountValidate()
{
	var id = document.getElementById('id').value;
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var passcode = document.getElementById('passcode').value;
	var posInd = document.employeeForm.position.selectedIndex;
	var position = document.employeeForm.position.options[posInd].text;
	
	var valid = true;
	
	if (passcode == 'j8&#4i^b/1!c')
	{
		passcode = '';
	}
	if(firstname == '')
	{
		valid = false;
		showFieldRequired('firstNameMessage');
	}
	if(lastname == '')
	{
		valid = false;
		showFieldRequired('lastNameMessage');
	}
	if(passcode == '')
	{
		passcode='0';
	}
	if (valid)
	{
		updateEmployeeAccountSend(id,username,password,firstname,lastname,position,passcode);
	}
	
}

function updateEmployeeAccountSend(id,username,password,firstname,lastname,position,passcode)
{
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('u') +
				'&id=' + encodeURI(id) +
				'&username='+encodeURI(username)+
				'&password='+encodeURI(password)+
				'&firstname='+encodeURI(firstname)+
				'&lastname='+encodeURI(lastname)+
				'&position='+encodeURI(position)+
				'&passcode='+encodeURI(passcode);
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				updateEmployeeAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				updateEmployeeAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function updateEmployeeAccountReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) 
	{	
		listAllEmployeeAccountSend();
		showDialog('Employee Account updated.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Update */

/* Delete */

function deleteEmployeeAccount () {
	deleteEmployeeAccountValidate();
}

function deleteEmployeeAccountValidate () {
	var id = document.getElementById('id').value;
	if (id == '') {
		//no item selected
		showDialog('No Employee Account is selected for deletion.');
	} else {
		//item selected
		deleteEmployeeAccountSend(id);
	}
}

function deleteEmployeeAccountSend (id) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('d') +
	            '&id=' + encodeURI(id);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				deleteEmployeeAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				deleteEmployeeAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function deleteEmployeeAccountReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		resetFields();
		listAllEmployeeAccountSend();
		showDialog('Employee Account deleted.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Delete */

/* List All */

function listAllEmployeeAccount () {
	listAllEmployeeAccountSend();
}

function listAllEmployeeAccountSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('l');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listAllEmployeeAccountReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listAllEmployeeAccountReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listAllEmployeeAccountReceive (xml) {
	if (xml.getElementsByTagName('employees').length > 0) {
		var employees = xml.getElementsByTagName('employees')[0];
		showResults(buildTable(employees));
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List All */
